package ui.model.card;

import java.util.ArrayList;
import java.util.List;

import business.entity.Card;
import business.entity.IVerifyCard;
import business.entity.VerifyCard;

import ui.model.ModelErrorInfo;
import ui.model.ViewModel;

public class EditCardViewModel extends ViewModel {

	// ViewModel持有的名片
	private Card card;

	// 用于验证名片的实例
	private IVerifyCard verifyCard;
	
	public EditCardViewModel() {
		verifyCard = new VerifyCard();
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	@Override
	public List<ModelErrorInfo> verifyModel() {
		List<ModelErrorInfo> error = new ArrayList<ModelErrorInfo>();
		
		if (card == null) {
			addErrorInfo(error, "名片为空", "card");
		} else {
			verifyCard.setCard(card);
			String errMsg = null;
			if ((errMsg = verifyCard.verifyCardName()) != null) {
				addErrorInfo(error, errMsg, "name");
			} else if ((errMsg = verifyCard.verifyCardTelephone()) != null) {
				addErrorInfo(error, errMsg, "telephone");
			} else if ((errMsg = verifyCard.verifyCardEmail()) != null) {
				addErrorInfo(error, errMsg, "email");
			} else if ((errMsg = verifyCard.verifyCardQQ()) != null) {
				addErrorInfo(error, errMsg, "qq");
			} else if ((errMsg = verifyCard.verifyCardWeibo()) != null) {
				addErrorInfo(error, errMsg, "weibo");
			}
		}
		return error;
	}

	private void addErrorInfo(List<ModelErrorInfo> list, String msg, String attr) {
		list.add(generateErrorInfo(msg, attr));
	}

	private ModelErrorInfo generateErrorInfo(String msg, String attr) {
		ModelErrorInfo error = new ModelErrorInfo();
		error.setErrMsg(msg);
		error.setErrAttName(attr);
		return error;
	}

}